package com.work.chat.custom.server.config.util;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.work.chat.common.eums.BizCodeEnum;
import com.work.chat.custom.server.config.LocalUserInfoAccessContext;
import com.work.chat.custom.server.config.exception.BizException;
import com.work.chat.custom.server.mapper.UserMapper;
import com.work.chat.custom.server.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class UserUtil {

    @Autowired
    private UserMapper userMapper;

    public  User getUser()
    {
        Long userId = LocalUserInfoAccessContext.getuserInfoAccessContext().getUserId();
        if (ObjectUtils.isEmpty(userId))
        {
            throw new BizException(BizCodeEnum.TOKEN_ERROR);
        }
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getId, userId));
        if (ObjectUtils.isEmpty(user))
        {
            throw new BizException(BizCodeEnum.USER_ERROR);
        }
        return user;
    }

}
